<template>
  <div class="container-content">
    <el-form
      ref="formRef"
      :model="formObject"
      label-width="100px"
      label-position="left"
    >
      <el-row :gutter="20">
        <!-- <el-col :span="12">
          <el-form-item label="合同类型">
            <el-select
              v-model="contractType"
              placeholder="请选择"
              style="width: 100%"
              @change="getContractTypeOptions"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="合同模版">
            <el-select
              v-model="contractTemp"
              placeholder="请选择"
              style="width: 100%"
              @change="getContractContent"
            >
              <el-option
                v-for="item in contractTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-card shadow="never">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="合同名称">
              <el-input v-model="formObject.contractName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同编号">
              <el-input v-model="formObject.contractNumber"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="需求子公司">
              <el-input v-model="formObject.deptId"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="合同金额">
              <el-input
                v-model="formObject.money"
                @change="changeMoney"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同金额大写" label-width="110px">
              <el-input v-model="formObject.moneyUpper"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="税率">
              <el-input v-model="formObject.taxRate"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="供应商Id">
              <el-input v-model="formObject.supplierId"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签订日期">
              <el-date-picker
                v-model="formObject.signTime"
                type="date"
                placeholder="选择日期"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="8">
            <el-form-item label="税率">
              <el-input v-model="formObject.taxRate"></el-input>
            </el-form-item>
          </el-col> -->
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="付款方式">
              <el-input v-model="formObject.payType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保证金金额" label-width="110px">
              <el-input v-model="formObject.marginMoney"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="formObject.remarks" type="textarea"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <Editor
        ref="editor"
        editor-id="editor"
        height="500px"
        :template-content="formObject.contractContent"
      />
    </el-form>
  </div>
</template>
<script>
import {
  getList,
  addProcure,
  editProcureList,
  getDetailProcure,
} from "@/api/system/contract";
import Editor from "../../editor/index.vue";
import { convertToRMB } from "@/utils/utils";

export default {
  components: {
    Editor,
  },
  props: ["id"],
  data() {
    return {
      contractType: "",
      contractTemp: "",
      contractTypeOptions: [],
      typeId: "",

      formObject: {
        contractContent: "",
        contractName: "",
        contractNumber: "",
        createBy: "",
        createTime: "",
        deptId: "",

        marginMoney: "",
        marginRate: "",
        money: "",
        moneyUpper: "",
        params: {},
        payType: "",
        projectId: "",
        remark: "",
        remarks: "",
        searchValue: "",
        signTime: "",
        supplierId: "",
        taxMoney: "",
        taxRate: "",
        type: 2,
        updateBy: "",
        updateTime: "",
      },
    };
  },
  methods: {
    getContractContent(val) {
      this.formObject.contractContent = this.contractTypeOptions.find(
        (item) => item.id == val
      ).content;
    },
    onSubmit() {
      if (this.id) {
        editProcureList(this.formObject).then((res) => {
          if (res.code === 200) {
            this.$message({
              message: "修改成功！",
              type: "success",
            });
          }
        });
      } else {
        addProcure(this.formObject).then((res) => {
          if (res.code === 200) {
            this.$message({
              message: "新增成功！",
              type: "success",
            });
          }
        });
      }
    },
    changeMoney() {
      this.formObject.moneyUpper = convertToRMB(this.formObject.money * 1);
    },
    // 获取详情回显
    getDetailData(id) {
      getDetailProcure(id).then((res) => {
        this.formObject = res.data;
        this.getContractTypeOptions(res.data.type);
      });
    },
  },
  created() {
    getList({
      typeId: 185,
    }).then((res) => {
      if (res.code === 200) {
        this.contractTypeOptions = res.rows.map((item) => {
          return {
            id: item.id,
            value: item.id,
            label: item.tempName,
            content: item.contractTempContent,
          };
        });
      }
    });
    // 获取详情回显
    if (this.id) {
      this.getDetailData(this.id);
    }
  },
};
</script>
<style lang="scss" scoped></style>
